package com.example.springboottpl.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboottpl.dao.PostDao;
import com.example.springboottpl.dao.UserPostDao;
import com.example.springboottpl.entity.PostBean;
import com.example.springboottpl.entity.UserPostBean;
import com.example.springboottpl.service.PostService;
import com.example.springboottpl.util.Result;
import com.example.springboottpl.util.ResultPage;
import com.example.springboottpl.vo.common.DeleteBatchReqVo;
import com.example.springboottpl.vo.common.QueryDetailReqVo;
import com.example.springboottpl.vo.common.UpdateStatusReq;
import com.example.springboottpl.vo.req.PostReqVo;
import com.example.springboottpl.vo.req.QueryPostReqVo;
import com.example.springboottpl.vo.resp.PostRespVo;

/**
 * 描述：岗位信息
 * 作者：刘飞华
 * 日期：2025/01/10 15:34:35
 */
@Service
public class PostServiceImpl implements PostService {

    @Autowired
    private PostDao postDao;
    @Autowired
    private UserPostDao userPostDao;

    /**
     * 添加岗位信息
     *
     * @param post 请求参数
     * @return int
     * @author 刘飞华
     * @date: 2025/01/10 15:34:35
     */
    @Override
    public Result<Integer> addPost(PostReqVo post) {

        if (postDao.selectCount(Wrappers.<PostBean>lambdaQuery().eq(PostBean::getPostCode, post.getPostCode())) > 0) {
            return Result.error("岗位编码已存在");
        }

        if (postDao.selectCount(Wrappers.<PostBean>lambdaQuery().eq(PostBean::getPostName, post.getPostName())) > 0) {
            return Result.error("岗位名称已存在");
        }

        PostBean bean = new PostBean();
        bean.setPostCode(post.getPostCode()); //岗位编码
        bean.setPostName(post.getPostName()); //岗位名称
        bean.setSort(post.getSort()); //显示顺序
        bean.setStatus(post.getStatus()); //岗位状态（0：停用，1:正常）
        bean.setRemark(post.getRemark()); //备注

        int i = postDao.insert(bean);
        return Result.success(i);
    }

    /**
     * 删除岗位信息
     *
     * @param post 请求参数
     * @return int
     * @author 刘飞华
     * @date: 2025/01/10 15:34:35
     */
    @Override
    public Result<Integer> deletePost(DeleteBatchReqVo post) {
        List<Long> ids = post.getIds();

        Long count = userPostDao.selectCount(Wrappers.<UserPostBean>lambdaQuery().in(UserPostBean::getPostId, ids));
        if (count > 0) {
            return Result.error("已分配,不能删除");
        }

        int i = postDao.deleteByIds(ids);
        return Result.success(i);
    }

    /**
     * 更新岗位信息
     *
     * @param post 请求参数
     * @return int
     * @author 刘飞华
     * @date: 2025/01/10 15:34:35
     */
    @Override
    public Result<Integer> updatePost(PostReqVo post) {
        PostBean res = postDao.selectById(post.getId());
        if (res == null) {
            return Result.error("岗位不存在");
        }

        if (postDao.selectCount(Wrappers.<PostBean>lambdaQuery()
                                        .eq(PostBean::getPostCode, post.getPostCode())
                                        .ne(PostBean::getId, post.getId())) > 0) {
            return Result.error("岗位编码已存在");
        }

        if (postDao.selectCount(Wrappers.<PostBean>lambdaQuery()
                                        .eq(PostBean::getPostName, post.getPostName())
                                        .ne(PostBean::getId, post.getId())) > 0) {
            return Result.error("岗位名称已存在");
        }


        PostBean bean = new PostBean();
        bean.setId(post.getId());//岗位id
        bean.setPostCode(post.getPostCode());//岗位编码
        bean.setPostName(post.getPostName());//岗位名称
        bean.setSort(post.getSort());//显示顺序
        bean.setStatus(post.getStatus());//岗位状态（0：停用，1:正常）
        bean.setRemark(post.getRemark());//备注

        int i = postDao.updateById(bean);
        return Result.success(i);
    }

    /**
     * 更新岗位信息状态
     *
     * @param post 请求参数
     * @return int
     * @author 刘飞华
     * @date: 2025/01/10 15:34:35
     */
    @Override
    public Result<Integer> updatePostStatus(UpdateStatusReq post) {

        LambdaUpdateWrapper<PostBean> wrapper = Wrappers.<PostBean>lambdaUpdate()
                .in(PostBean::getId, post.getIds())
                .set(PostBean::getStatus, post.getStatus());
        int i = postDao.update(wrapper);
        return Result.success(i);
    }

    /**
     * 查询岗位信息详情
     *
     * @param post 请求参数
     * @return PostResp
     * @author 刘飞华
     * @date: 2025/01/10 15:34:35
     */
    @Override
    public Result<PostRespVo> queryPostDetail(QueryDetailReqVo post) {

        PostBean postBean = postDao.selectById(post.getId());
        if (postBean == null) {
            return Result.error("岗位信息不存在");
        }

        PostRespVo resp = new PostRespVo();
        resp.setId(postBean.getId());//岗位id
        resp.setPostCode(postBean.getPostCode());//岗位编码
        resp.setPostName(postBean.getPostName());//岗位名称
        resp.setSort(postBean.getSort());//显示顺序
        resp.setStatus(postBean.getStatus());//岗位状态（0：停用，1:正常）
        resp.setRemark(postBean.getRemark());//备注
        resp.setCreateTime(postBean.getCreateTime());//创建时间
        resp.setUpdateTime(postBean.getUpdateTime());//更新时间

        return Result.success(resp);
    }

    /**
     * 查询岗位信息列表
     *
     * @param post 请求参数
     * @return PostResp
     * @author 刘飞华
     * @date: 2025/01/10 15:34:35
     */
    @Override
    public Result<ResultPage<PostRespVo>> queryPostList(QueryPostReqVo post) {
        LambdaQueryWrapper<PostBean> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.like(post.getPostName() != null, PostBean::getPostName, post.getPostName());
        queryWrapper.like(post.getPostCode() != null, PostBean::getPostCode, post.getPostCode());
        queryWrapper.eq(post.getStatus() != null, PostBean::getStatus, post.getStatus());

        Page<PostBean> page = postDao.selectPage(new Page<>(post.getPageNo(), post.getPageSize()), queryWrapper);

        List<PostRespVo> list = page.getRecords().stream().map(x -> {
            PostRespVo resp = new PostRespVo();
            resp.setId(x.getId());//岗位id
            resp.setPostCode(x.getPostCode());//岗位编码
            resp.setPostName(x.getPostName());//岗位名称
            resp.setSort(x.getSort());//显示顺序
            resp.setStatus(x.getStatus());//岗位状态（0：停用，1:正常）
            resp.setRemark(x.getRemark());//备注
            resp.setCreateTime(x.getCreateTime());//创建时间
            resp.setUpdateTime(x.getUpdateTime());//更新时间
            return resp;
        }).collect(Collectors.toList());

        return Result.success(new ResultPage<>(list, page.getCurrent(), page.getSize(), page.getTotal()));

    }
}